﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Diagnostics;

namespace WordList
{
    public class UserService
    {
        public static MySqlConnection GetConnection()
        {
            MySqlConnection connection;
            try { 
             connection = new MySqlConnection(
                "datasource=localhost;username=root;" +
                "password=13603466636;database=word1;charset=utf8");
            connection.Open();
            }
            catch (Exception err)//LiHaoyang's mysql env
			{
				Debug.WriteLine(err.Message);
				try
				{
					connection = new MySqlConnection(
					"datasource=localhost;username=root;" +
					"password=1234;database=wollocaldb;charset=utf8");
					connection.Open();
				}
				catch
				{

					connection = new MySqlConnection(
				   "datasource=localhost;username=root;" +
				   "password=root;database=dictionary;charset=utf8");
					connection.Open();
				}
			}
            return connection;
        }
        public static void CreateWordList(VocUser user,int listId)
        {
            using(MySqlConnection conn = GetConnection())
            {
                string sql = "select * from cet";
                using (MySqlCommand command = new MySqlCommand(sql, conn))
                {
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        WordList wordList = new WordList(listId);
                        List<Word> words = new List<Word>();
                        while (reader.Read())
                        {
                            if (reader.HasRows)
                            {
                                if (reader.GetInt32(0) % 4 == 0)
                                {
                                    words.Add(new Word(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), 1, 1));
                                }
                                else if (reader.GetInt32(0) % 3 == 0)
                                {
                                    words.Add(new Word(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), 0, 1));
                                }
                                else
                                {
                                    words.Add(new Word(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), 0, 0));
                                }
                            }
                        }
                        wordList.List = words;
                        user.List.Add(wordList);
                    }
                }
            }
        }
        public static List<Word> GetWordList(VocUser user, int listId)
        {
            if (listId <= 0 || listId > user.List.Count)
            {
                Debug.WriteLine("单词本序号不合法:");
                listId = 1;
            }
            var wl = user.List.FirstOrDefault(s => s.Id == listId);
            return wl.List;
        }
    }
}
